<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$group=pdo_getall('jy_lineks_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$groupid=intval($_RQ['groupid']);
	if($groupid){
		$params['groupid']=$groupid;
		$return['params']['groupid']=$groupid;
	}
	$groupid2=intval($_RQ['groupid2']);
	if($groupid2){
		$params['groupid2']=$groupid2;
		$return['params']['groupid2']=$groupid2;
	}
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby=array();
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['num']){
		$psize = $_RQ['num'];
	}else{
		$psize = 10;
	}
	$total='';
	$list=pdo_getslice('jy_lineks_tiku_zu',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['desc'] = $item['desc'] ? $item['desc'] : cfc('tools')->substr($item['content'],0,36);
			$item['group']=$group[$item['groupid']]['title'];			
		}
	}
	$return['groupname']=$group[$groupid]['title'];
	$return['list']=$list?$list:array();
	$return['group']=$group?$group:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'题库列表'),
			array('op'=>'edit','title'=>'添加题库'),
			//array('op'=>'daoru','title'=>'导入题库'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'image','title'=>'题库LOGO','type'=>'image'),
			array('name'=>'title','title'=>'题库标题','type'=>'string'),
			array('type'=>'select','name'=>'groupid','options'=>'allgroups','title'=>'一分类'),
			array('type'=>'select','name'=>'groupid2','options'=>'allgroups','title'=>'二分类'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
			array('name'=>'isvip','title'=>'权限','type'=>'radio','options'=>'isvip'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		),'action'=>array(
			array('type'=>'export','action'=>'export','title'=>'导出题目'),
			array('type'=>'href','op'=>'daoru','title'=>'导入题目'),
			array('type'=>'href','action'=>'/tiku/','title'=>'题目列表'),
			array('type'=>'post','op'=>'qingchu','message'=>'确定清空所有题目吗？','title'=>'清空题目'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>10,'title'=>'题库标题','type'=>'string'),
			array('type'=>'select','width'=>10,'name'=>'groupid','options'=>'groups','title'=>'一分类'),
			array('type'=>'select','width'=>10,'title'=>'二级分类','name'=>'groupid2','options'=>'groups[pagedata.params.groupid].pici'),
		),
	);
	$return['isvip']=array('免费','VIP');
	$return['htmls']=$htmls;
	$return['allgroups']=pdo_getall('jy_lineks_group',array('uniacid'=>$_SESSION['uniacid']),array('title','id'),'id');
	$return['groups']=pdo_getall('jy_lineks_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>0),array('title','id'),'id');
	foreach($return['groups'] as &$gv){
		$gv['pici']=pdo_getall('jy_lineks_group',array('parentid'=>$gv['id'],'uniacid'=>$_SESSION['uniacid']),array('title','id'),'id');
	}
	$return['types']=$this->ti_types;
	exi($return);
}
if($_RQ['op']=='set'){
	$post=get_postdata();
	$id=intval($_RQ['id']);
	pdo_update('jy_lineks_tiku_zu',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('操作成功！');
}
if($_RQ['op']=='qingchu'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_lineks_tiku',array('zu_id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('清空题目成功！');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_lineks_tiku_zu',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_lineks_tiku_zu',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_lineks_tiku_zu',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array('status'=>1);
	}
	$item['xuanxiang']=json_decode($item['xuanxiang'],true);
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回题库列表'),
		),
		'edit'=>array(
					array('name'=>'paixu','title'=>'排序','type'=>'string'),
					array('name'=>'title','title'=>'题库标题','type'=>'string','must'=>true),
					array('name'=>'name','title'=>'考试类型','type'=>'string','help'=>'默认：模拟考试'),
					array('name'=>'image','title'=>'题库LOGO','type'=>'image'),
					array('name'=>'desc','title'=>'题库描述','type'=>'text'),
					array('type'=>'select2','name'=>'groupid','options'=>'groups','title'=>'一级分类',
						'name2'=>'groupid2','options2'=>'pici','title2'=>'二级分类',
					),
					array('name'=>'isvip','title'=>'权限','type'=>'radio','options'=>'isvip'),
					//array('name'=>'price','title'=>'题库费用','type'=>'string'),
					array('name'=>'needcard','title'=>'需要绑定答题卡？','type'=>'switch','help'=>'绑定了答题卡才能练习题库！'),
					array('name'=>'liandays','title'=>'练习天数','type'=>'unitstring','unit'=>'天','help'=>'绑卡后，开始倒计时！'),
					array('name'=>'status','title'=>'状态','type'=>'switch'),
		)
	);
	$return['isvip']=array('免费','VIP');
	$return['types']=$this->ti_types;
	$return['danan_key']=$this->danan_key;
	$return['duicuo']=$this->duicuo;
	$return['groups']=pdo_getall('jy_lineks_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>0),array('title','id'),'id');
	foreach($return['groups'] as &$gv){
		$gv['pici']=pdo_getall('jy_lineks_group',array('parentid'=>$gv['id'],'uniacid'=>$_SESSION['uniacid']),array('title','id'),'id');
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$fields=array('title','name','isvip','type','desc','image','groupid','groupid2','paixu','status','price','needcard','liandays');
	$data=array_fields($fields,$postdata);
	if(empty($data['title'])){
		exi('请填写题库标题！','error');
	}
	if(empty($data['groupid'])){
		exi('请填写题库分类！','error');
	}
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_lineks_tiku_zu',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('编辑题库成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_lineks_tiku_zu',$data);
		$id=pdo_insertid();
		exi('添加题库成功！','','list');
	}
}
if($_RQ['op']=='daoru'){
	$return=array();
	if($_RQ['postdata']){
		$postdata=get_postdata();
		if(empty($postdata['excel'])){
			exi('请上传题目CSV！','error');
		}
		if(empty($postdata['zu_id'])){
			exi('请选择题库！~~'.$postdata['zu_id'],'error');
		}
		
		$excelfeil=cfc('files')->local($postdata['excel']);
		$excel=cfc('tools')->importExcel($excelfeil);
		$res=$this->model('model')->get_csvitems($excel);
		if(!$res[0]['title']){
			$excel=cfc('tools')->importExcel($excelfeil,true);
			$res=$this->model('model')->get_csvitems($excel);	
		}
		if(!$res[0]['title']){
			exi('题目异常无法导入！','error');
		}
		cfc('files')->source_del($postdata['excel']);
		$res=array_reverse($res);
		foreach($res as &$rv){
			$value=str_array($rv['value'],'|');
			$rv['xuanxiang']=array();
			foreach($value as $vv){
				$rv['xuanxiang'][]=array('title'=>$vv);
			}
			if($rv['type']=='单选题'){
				$rv['type']=4;
			}
			if($rv['type']=='多选题'){
				$rv['type']=5;
			}
			if($rv['type']=='判断题'){
				$rv['type']=20;
			}
			$isadd=pdo_get('jy_lineks_tiku',array('zu_id'=>$postdata['zu_id'],'title'=>$rv['title'],'type'=>$rv['type']));
			if($isadd['id']){
				//continue;
			}
			$images=str_array($rv['images'],'|');
			$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'zu_id'=>$postdata['zu_id'],
				'title'=>$rv['title'],
				'images'=>ijson_encode($images),
				'desc'=>$rv['desc'],
				'type'=>$rv['type'],
				'score'=>$rv['score'],
				'xuanxiang'=>ijson_encode($rv['xuanxiang']),
				'daan'=>$rv['daan'],
				'status'=>1,
				'createtime'=>TIMESTAMP,
			);
			pdo_insert('jy_lineks_tiku',$data);
		}
		exi('导入成功！','','list');
	}
	$item['excelurl']=SITEROOT.'/addons/jy_lineks/resource/test.csv';
	if($_RQ['id']){
		$item['zu_id']=$_RQ['id'];
	}else{
		exi('请选择题库.','error');
	}
	//print_r($item);exit;
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回题库列表'),
		),
		'edit'=>array(
			array('name' => 'excelurl','type' => 'copy','title' => 'CSV示例文件下载'),
			array('name' => 'excel','type' => 'upfile','title' => '上传题目CSV','help'=>'<red>题目相同的将不被导入！</red>'),
				//array('type'=>'select','name'=>'zu_id','options'=>'tiku_zu','title'=>'选择题库'),
		)
	);
	$return['types']=$this->ti_types;
	$return['duicuo']=$this->duicuo;
	$return['tiku_zu']=pdo_getall('jy_lineks_tiku_zu',array('uniacid'=>$_SESSION['uniacid']),array('title','id'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='export'){
	if(empty($_RQ['id'])){
		exi('请选择题库！','error');
	}
	$params=array('uniacid'=>$_SESSION['uniacid'],'zu_id'=>$_RQ['id']);
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$this->model('model')->down_csv($params);
}
